<?php
   class File{
   	
   	/*
	 * $type：上传文件的分类
	 * $name:上传form表单file的name值
	 */
   	static public function uploadFile($name,$type,$use,$isupdate = false){
   		$file_path = DB::fetch_row("select * from `bb_file_path` where `type`='{$type}' and `use`='{$use}'");
		$path       = $file_path['path'];
		$uploadFile = $_FILES[$name];
		$tmpName    = $uploadFile['tmp_name'];
		$fileSize   = $uploadFile['size'];
		$fileType   = $uploadFile['type'];
		$fileError  = $uploadFile['error'];
        
		$file  =  array();
		$file['filename'] =  $uploadFile['name'];
		if($use=='static'){
			$file['realname'] =  $path.$uploadFile['name'];
		}else{
			//$file['realname'] =  $path.time().strrchr($uploadFile['name'],".");
			$file['realname'] =  $path.md5(uniqid(microtime())).strrchr($uploadFile['name'],".");
		}
		$file['filesize']     =  $fileSize;
		$file['filetype']     =  strtolower(strrchr($uploadFile['name'],"."));
		$file['type']         =  $type;
		$file['pid']      = $file_path['pid'];
		$file['createtime'] = time();
		
		$ok = 0;
		switch($file['filetype']){
			case ".txt":$ok=1;break;
	        case ".doc":$ok=1;break;
			case ".docx":$ok=1;break;
	        case ".xls":$ok=1;break;
	        case ".ppt":$ok=1;break;
            case ".jpg":$ok=1;break;
	        case ".jpeg":$ok=1;break;
	        case ".png":$ok=1;break;
	        case ".gif":$ok=1;break;
	        case ".bmp":$ok=1;break;
		}
		if($fileError)
			return "File error";
		else if(!$ok)
			return "File type no permit";
		else{
			move_uploaded_file($tmpName,$file['realname']);
			if(!$isupdate)
			    return DB::insert("bb_file", $file);
			else
				return $file['realname'];
		}
		return "Data base error";
   	}
   /*
    * 移动文件
    */  
    static public function move_file($old_filename , $type ,$use){
    	$old_filearray = explode("/",$old_filename);
		$filename = $old_filearray[count($old_filearray)-1];
		$file_path = DB::fetch_row("select *from `bb_file_path` where `type`='{$type}' and `use` = '{$use}'");
		$new_filename = $file_path['path'].$filename;
		copy($old_filename,$new_filename);
		self::deleteFile($old_filename, "delete file");
		$condition = array();
		$condition['realname'] = $old_filename;
		$file = array();
		$file['realname'] = $new_filename;
		$file['type']     = $type;
		if(DB::update("bb_file", $file, $condition)){
			return $new_filename;
		}else{
			return 0;
		}
    }
	/*
	 * 删除文件
	 */  
	static public function deleteFile($path,$type){
		if($type=="delete dir"){ 
			if(!is_dir($path))
				return false;
			self::_deleteDir($path);
		}
		if($type=="delete files"){
			if(!is_dir($path))
				return false;
			self::_deleteFiles($path);
		}		
		if($type=="delete file"){//删除单个文件
			if(!is_file($path))
				return false;
			unlink($path);
		}
	}
	
	static private function _deleteDir($dir){ //删除目录及目录下的所有文件夹和文件
		$dir_arr = scandir($dir);
		foreach($dir_arr as $key=>$val){
			 if($val == '.' || $val == '..'){}
             else
             {
             	if(is_dir($dir.'/'.$val))
             	{
             		if(@rmdir($dir.'/'.$val) == 'true'){}
					else{self::_deleteDir($dir.'/'.$val);}
             	}
				else unlink($dir.'/'.$val);
             }
		}
		rmdir($dir);
	}
	
	static private function _deleteFiles($dir){  //仅删除该目录下的文件，不删除该目录和目录下的文件夹
		$dir_arr = scandir($dir);
		foreach($dir_arr as $key=>$val){
			if($val == '.' || $val == '..'){}
			else
			{
				if(!is_dir($dir.'/'.$val))
					unlink($dir.'/'.$val);
			}
		}
	}
	
	static function updateFile($type){
		
	}
	
	/*
	 * $path:所要创建的一系列文件夹
	 */
	static public function createFolder($path){
		if(!file_exists($path)){
			self::createFolder(dirname($path));
			mkdir($path, 0777);
		}
	}
	
	static public function createText($fileName,$con){
		$result = 0;
		$fo = fopen($fileName,"w");
		$result = fwrite($fo,$con);
		return $result;
	}
   }
   

?>